home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Mode Examples / Perl-Example.pl < prev    next >
Encoding:
Perl Script  |  2000-10-30  |  9.9 KB  |  305 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Perl-Example.pl
  4. # included in the Alpha distribution as an example of the Perl mode
  5. # Perl mode is part of Alpha's standard distribution
  6. # More information about Perl and MacPerl can be found at
  7. #      <http://www.macperl.com/>
  8. #      
  9. # Note: if you use Setext mode, this Perl script can be used to convert
  10. # Setext to HTML.  Some customization is available in the first code block.
  11. #      -Donavan Hall
  12. #      
  13. #########################################################################
  14. # Setext2HTML.pl
  15. # Convert setext input file to html index plus 1 file per chapter.
  16. # NOTE: Input text must be wrapped text (not word processor paragraphs).
  17. # Refer to EasyView documentation for setext details, or anon ftp
  18. #          ftp://garbo.uwasa.fi/mac/tidbits/setext/setext_concepts_Aug92.etx
  19. #
  20. # Sandra Silcot, csg1, ITS, University of Melbourne
  21. # Email: ssilcot@ariel.unimelb.edu.au
  22. #########################################################################
  23. # CUSTOMISE These as required
  24.  
  25. $title             = "MACSCRPT Digests";
  26. $parttitle         = "MACSCRPT Digest :";
  27. $source = 'Mail digests --> EasyView --> Setext --> Setext2HTML (S.Silcot)';
  28. $datemodified      = '23-Sep-1994.';
  29. $maintainer        = 'Sandra Silcot';
  30. $maintaineremail   = 'ssilcot@www.unimelb.edu.au';
  31. $indexpointers = "<pre><a href=\"ghindex.html\"><b>Search</b></a>  |  ".
  32. "<a href=\"http://www.unimelb.edu.au/~ssilcot/mail-archive/index.html\">Mail-index</a>  |  ".
  33. "<a href=\"http://www.unimelb.edu.au/~ssilcot/index.html\">Sandra's-home</a>  |  ".
  34. "<a href=\"http://www.unimelb.edu.au/\">UniMelb-CWIS</a></pre>";
  35.  
  36. ########################################################### 
  37. # No customisation needed below this line
  38. ########################################################### Init
  39. $Url        = '(http://|ftp://|afs://|wais://|telnet://|gopher://|' .
  40.           'news:|nntp:|mid:|cid:|mailto:|prospero:)';
  41. $UrlExp    = $Url . q%[^\s\(\)\|<>"']*[^\.;,"'\|\[\]\(\)\s<>]%;
  42. $HUrlExp   = $Url . q%[^\s\(\)\|<>"'\&]*[^\.;,"'\|\[\]\(\)\s<>\&]%;
  43.  
  44.  
  45. $q = '"';              # double quote
  46. require "ctime.pl";
  47. $DateTime = &ctime(time);
  48. chop($DateTime);
  49. if( $MacPerl'Version ) {$pathdelim = ":";}
  50. else                   {$pathdelim = "/";}
  51. @filenameparts = split(/$pathdelim/,$ARGV[0]);
  52. $infile= $filenameparts[$#filenameparts];
  53. if (!infile) {die ("ÄÄÄNO INPUT FILE");}
  54.  
  55. $footer = "\n<hr>\n" .
  56.           "<pre>HTML generated by " .
  57.           "<a href=\"http://www.unimelb.edu.au/~ssilcot/ssperltools/Setext2html.html\">Setext2html.pl</a>".
  58.           " $DateTime.\n" .
  59.           "Original source: $source\n" .
  60.           "<address>$maintainer\n" .
  61.           "<a href=\"mailto:$maintaineremail\">$maintaineremail</a></address>\n" .
  62.           "</pre></body>\n</html>\n";
  63.  
  64. $CHAPTER = 1;
  65. $SECTION = 2;
  66. $BODY    = 3;
  67. $SKIP    = 0;
  68.  
  69. ########################################################### glob input file
  70.  
  71. open (STDIN,"$ARGV[0]") || die ("ÄÄÄFAILED TO OPEN INPUT FILE $ARGV[0]");
  72.  
  73. #$out = "<li><a href=$q".$infile."_$cnum.html#$i$q>$_</a></li>\n";
  74. #print $out;
  75. #exit;
  76.  
  77. # read in the whole file, do bolds and translate specials
  78.  
  79. $i = 0;
  80. for (<STDIN>) {
  81. #    s~<~<~g;
  82. #    s~>~>~g;
  83.     s~\*\*([^\*]+)\*\*~<b>$1</b>~g;  # setext bolds wrapped like **foo**
  84.     tr/Ä≥å≤πÅ/*"'"'/;                # translate specials
  85.     $data[$i] = $_;
  86.     $i++;
  87. }
  88.  
  89. ########################################################### pass 1 - set flags
  90.  
  91. $prev = "";
  92. $nchapters = 0;
  93.  
  94. for ($i = 0; $i <= $#data; $i++) {
  95.     $before = $i - 1;
  96.     $_ = $data[$i];                 # $_ is default
  97.     $thislength = length;
  98.     if (($thislength==$prevlength) && (m/^====/i)) {   # preceeding line a chapter
  99.         if ($nchapters == 0) {    
  100.             $docname = $prev;    # first chapter is document name
  101.             chop($docname);
  102.             } 
  103.         $nchapters++;
  104.         $chap{$i} = $prev;
  105.         $flag[$before] = $CHAPTER;
  106.         chop($data[$before]);
  107.         $flag[$i] = $SKIP;
  108.         }
  109.     elsif (($thislength==$prevlength) && (m/^----/i)) {   # preceeding line a section
  110.         $sect{$i} = $prev;
  111.         $flag[$before] = $SECTION;
  112.         chop($data[$before]); 
  113.         $flag[$i] = $SKIP;
  114.         }
  115.     else {
  116.         $flag[$i] = $BODY;        
  117.     }
  118.     $prev = $_;
  119.     $prevlength = length;
  120. }
  121.  
  122. ########################################################### htmlise
  123. # THANKS to Earl Hood - his MHonArc supplies the code which htmlises lines
  124. for ($i = 0; $i <= $#data; $i++) {
  125. ####    $data[$i] =~ s~<~<~g;
  126. ####    $data[$i] =~ s~>~>~g;
  127.     # convert to html
  128.     $data[$i] = &convert_line($data[$i]);
  129.     # do mail to urls (ex mhonarc by E.Hood
  130.        $data[$i] =~ s|([\!\%\w\.-]+@[\w\.-]+)|<A HREF="mailto:$1">$1</A>|g;
  131.  
  132. }
  133.  
  134. ########################################################### do chapter index file
  135.  
  136. open (STDOUT,">$ARGV[0].html") || die ("ÄÄÄFAILED TO OPEN OUTPUT FILE $ARGV[0].html");
  137. print "<html><head>\n<link rev=\"made\" href=\"mailto:$maintaineremail\">\n" .
  138.       "<title>$title</title>\n</head>\n<body>\n\n";
  139. print "$indexpointers\n<hr>\n<h1>$title</h1>\n\n<ul>\n";
  140.  
  141.  
  142. # now pass through all the lines again for lists in TOC file
  143.  
  144. $lastischap = 1;
  145. $cnum = 9;
  146.  
  147. for ($i = 0; $i <= $#data; $i++) {
  148.     $_ = $data[$i]; 
  149.     if  ($flag[$i] == $CHAPTER) {   # its a chapter
  150.         $cnum++;
  151.         print "<li><a href=$q".$infile."_$cnum.html#$i$q>$_</a></li>\n";
  152.         }
  153. }
  154. print "  </ul>\n<hr>$indexpointers\n\n$footer";
  155.  
  156. ########################################################### chapter contents
  157. # now pass through all the lines again and print heads and body 
  158. # ONE FILE PER CHAPTER - print section contents to each file
  159.  
  160. $cnum = 9;
  161. $end ="";
  162.  
  163. for ($i = 0; $i <= $#data; $i++) {
  164.     $_ = $data[$i];   
  165.     if  ($flag[$i] == $CHAPTER) {   
  166.         #
  167.         # NEW CHAPTER - finish off last and start a new output file
  168.         #
  169.         $cnum++;
  170.         if ($cnum == 10) {
  171.             $prev = $infile. ".html";  # link back to index if first chapter
  172.             }
  173.         else {
  174.             $x = $cnum - 1;
  175.             $prev = $infile. "_$x.html";
  176.             }
  177.         $x = $cnum + 1;
  178.         $next = $infile. "_$x.html";
  179.         #
  180.         # finish off last file
  181.         #
  182.         print "$end"; 
  183.         if ($nchapters > 1) {
  184.            if ($cnum == ($nchapters + 9)) {                    # last chapter
  185.                $navigation[$cnum] = "<hr><pre><a href=$q$prev$q><b>Previous-digest</b></a>  |  " .
  186.                                     "<a href=$q$infile.html$q><b>Digest-index</b></a>\n\n".
  187.                                     "$indexpointers\n";
  188.                }
  189.            elsif ($cnum == 10) {                               # first chapter
  190.                $navigation[$cnum] = "<hr><pre><a href=$q$next$q><b>Next-digest</b></a>  |  " .
  191.                                     "<a href=$q$infile.html$q><b>Digest-index</b></a>\n\n".
  192.                                     "$indexpointers\n";
  193.                }
  194.            else {
  195.                $navigation[$cnum] = "<hr><pre><a href=$q$next$q><b>Next-digest</b></a>  |  " .
  196.                                     "<a href=$q$prev$q><b>Previous-digest</b></a>  |  " .
  197.                                     "<a href=$q$infile.html$q><b>Digest-index</b></a>\n\n".
  198.                                     "$indexpointers\n";
  199.                }
  200.            }
  201.         else {$navigation[$cnum] = "";}
  202.         $end = "</ul>\n$navigation[$cnum]\n<hr><pre>";
  203.         close (STDOUT);
  204.         #
  205.         # start next file
  206.         #
  207.         open (STDOUT,">$ARGV[0]_$cnum.html") || die ("ÄÄÄFAILED TO OPEN $ARGV[0]_$cnum.html");
  208.         if ($nchapters > 1) {
  209.             print "<html><head>\n" .
  210.                   "<link rev=\"made\" href=\"mailto:$maintaineremail\">\n" .
  211.                   "<title>$title - $_</title>\n</head>\n<body>\n\n";
  212.             }
  213.         else {
  214.             print "<html><head>\n" .
  215.                   "<link rev=\"made\" href=\"mailto:$maintaineremail\">\n" .
  216.                   "<title>$title</title>\n</head>\n<body>\n\n";
  217.             }
  218.         print "$indexpointers\n<hr>\n<h1>$parttitle $_</h1>\n\n<ul>\n";
  219.         }
  220.     elsif  ($flag[$i] == $SECTION) {   # its a section
  221.         #
  222.         # NEW SECTION
  223.         #
  224.         print "<li><a href=#$i>$_</a></li>\n";
  225.         }
  226. }
  227.  
  228. ########################################################### append chapter body
  229. # now pass through all the lines again and print headers for sections and body 
  230. # APPEND TO EACH CHAPTER
  231.  
  232. $cnum = 9;
  233.  
  234. for ($i = 0; $i <= $#data; $i++) {
  235.     $_ = $data[$i];   
  236.     if  ($flag[$i] == $CHAPTER) {   # its a chapter
  237.  
  238.         $cnum++;     
  239.         # print end stuff
  240.         print "$end";
  241.         $end = "</pre>\n<p></p>\n$navigation[$cnum]\n$footer";
  242.         close (STDOUT);
  243.  
  244.         # append heads and body
  245.  
  246.         open (STDOUT,">>$ARGV[0]_$cnum.html") || die ("ÄÄÄFAILED TO OPEN $ARGV[0]_$cnum.html");
  247.         
  248.         }
  249.     elsif  ($flag[$i] == $SECTION) {   # its a section
  250.         print "</pre>\n\n<hr><h2><a name=$i>$_</a></h2>\n<pre>\n";
  251.         }
  252.     elsif  ($flag[$i] == $BODY) {   # its a body line 
  253.         print;
  254.         }
  255. }
  256.  
  257. # fin
  258. print "$end";
  259. close (STDOUT);
  260.  
  261. ##---------------------------------------------------------------------------
  262. ##    convert_line() translates a line to HTML.  Checks are made for
  263. ##    embedded URLs.
  264. sub convert_line {
  265.     local($tmp) = shift;
  266.     local($item, $item2, @array);
  267.  
  268.     if (!$NOURL) {
  269.     if (@array = split(m%($Url[^\s\(\)\|<>"']*[^\.;,"'\|\[\]\(\)\s<>])%o,
  270.                    $tmp)) {
  271.         $tmp = '';
  272.         while($#array > 0) {
  273.         $item = shift @array;
  274.         &htmlize(*item);
  275.         $item2 = shift @array;
  276.         $tmp .= $item . '<A HREF="' . $item2 . '">' . $item2 . '</A>';
  277.  
  278.         # The next line is needed since Perl's split function also
  279.         # returns extra entries for nested ()'s in the split pattern.
  280.         shift @array  if $array[0] =~ m%^$Url$%o;
  281.         }
  282.         $item = shift @array;
  283.         &htmlize(*item);
  284.         $tmp .= $item;
  285.     ## Regular line
  286.     } else {
  287.         &htmlize(*tmp);
  288.     }
  289.     } else {
  290.     &htmlize(*tmp);
  291.     }
  292.     $tmp;
  293. }
  294. ##---------------------------------------------------------------------------
  295. ##    "Entify" special characters
  296. sub htmlize {
  297.     local(*txt) = shift;
  298.     $txt =~ s/&/\&/g;
  299.     $txt =~ s/>/>/g;
  300.     $txt =~ s/</</g;
  301.     $txt;
  302. }
  303.  
  304.